package com.ls.markt.util;

import com.alibaba.excel.EasyExcel;
import com.ls.markt.listener.excel.ExcelListener;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

public class ExcelUtil {

    public static List<Object> readExcel(MultipartFile excel, Class c) throws IOException {
        ExcelListener excelListener = new ExcelListener();
        EasyExcel.read(excel.getInputStream(), c, excelListener).sheet().doRead();
        return excelListener.getDataList();
    }


//    public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) {
//        ExcelListener excelListener = new ExcelListener();
//        ExcelReader reader = getReader(excel,excelListener);
//
//        if (reader == null) {
//            return null;
//        }
//
//        reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass()));
//
//        return excelListener.getDataList();
//    }


    /**
     * 返回 ExcelReader
     *
     * @param excel 需要解析的 Excel 文件
     * @param excelListener new ExcelListener()
     */
//    private static ExcelReader getReader(MultipartFile excel, ExcelListener excelListener) {
//        String filename = excel.getOriginalFilename();
//
//        if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
////            throw new Exception("文件格式错误！");
//            return null;
//        }
//        InputStream inputStream;
//
//        try {
//            inputStream = new BufferedInputStream(excel.getInputStream());
//
//            return new ExcelReader(inputStream, null, excelListener, false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return null;
//
//    }

}
